package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao.healthEducationDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.dao_impl.healthEducationDaoImpl;
import com.example.yiwei.crm.data.GetClue;
import com.example.yiwei.crm.data.GetClueData;
import com.example.yiwei.crm.data.GetCoorData;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.entity.ClueMeesageEntity;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ClueManagerListAdapter;
import com.example.yiwei.crm.view.Class.listviewClass.ListAdapter;
import com.example.yiwei.crm.view.activity.coordinationActivity.DetailedCoordinationActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.SimpleCoordinationActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.example.yiwei.crm.R.id.health_education_img_back;


public class HealthSimpleClueMnagerActivity extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private ImageView img_back, img_add,img_search;
    private AutoListView lstv;
    private ClueManagerListAdapter ladapter;
    SharedPreferences sp;
    int page=1;
    int a = 1;
    List<ClueMeesageEntity> list;
    List<ClueMeesageEntity> list1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ClueMeesageEntity> result = (List<ClueMeesageEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    list.addAll(result);
                    if (a==1){
                        ladapter = new ClueManagerListAdapter(HealthSimpleClueMnagerActivity.this, list);
                        lstv.setAdapter(ladapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    if (result!=null){
                        lstv.onLoadComplete();
                        list.addAll(result);
                    }else {
                        lstv.onLoadComplete();
                    }

            }
//            lstv.setResultSize(result.size());
            ladapter.notifyDataSetChanged();
        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_simple_clue_mnager);
        img_search = (ImageView) findViewById(R.id.health_education_img_search);
        lstv= (AutoListView) findViewById(R.id.simple_health_education_listview);
        img_add= (ImageView) findViewById(R.id.health_education_img_add);
        img_back= (ImageView) findViewById(R.id.health_education_img_back);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        list=new ArrayList<>();
        lstv.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        lstv.setDividerHeight(1);
        lstv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lstv.setOnRefreshListener(this);
        lstv.setOnLoadListener(this);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("KRA01",2061);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                healthEducationDao healthEducationDao = new healthEducationDaoImpl();
                list= healthEducationDao.queryHealthClueMessage(baseClass.find_healthEducation_clue_url+1,json);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//

                        if (list != null) {
//                            btn_add_visit.setVisibility(View.VISIBLE);
//                            LoadingActivity.l.finish();
                            ladapter = new ClueManagerListAdapter(HealthSimpleClueMnagerActivity.this, list);
                            lstv.setAdapter(ladapter);
                            ladapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(HealthSimpleClueMnagerActivity.this, "没有信息", Toast.LENGTH_SHORT).show();
                        }
//                        loadData(AutoListView.REFRESH);
                    }
                });
            }
        }).start();
        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HealthSimpleClueMnagerActivity.this,HealthAddClueActivity.class);
                startActivity(intent);
                finish();
            }
        });
        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                        GetClue.create();
                GetClue.clue = list.get(i - 1);
                        if (! GetClue.clue.toString().equals("")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent(HealthSimpleClueMnagerActivity.this, HealthDetailedClueManagerActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }

            }
        });
    }

    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
           UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
           LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                object.put("KRA01",2061);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            healthEducationDao healthEducationDao = new healthEducationDaoImpl();
            list1= healthEducationDao.queryHealthClueMessage(baseClass.find_healthEducation_clue_url+1,json);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (list1!=null){
                if (lstv.getVisibility()==View.GONE){
                    lstv.setVisibility(View.VISIBLE);
                }
                msg.obj = list1;
                handler.sendMessage(msg);
            }else {
                lstv.setVisibility(View.GONE);
                Toast.makeText(HealthSimpleClueMnagerActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                object.put("KRA01",2061);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            healthEducationDao healthEducationDao = new healthEducationDaoImpl();
            list1= healthEducationDao.queryHealthClueMessage(baseClass.find_healthEducation_clue_url+page,json);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (list1!=null){
                msg.obj = list1;
                handler.sendMessage(msg);
            }else {
                msg.obj = list1;
                handler.sendMessage(msg);
                Toast.makeText(HealthSimpleClueMnagerActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetCoorData.get()!=null){
            list = GetClueData.clue;
            ladapter = new ClueManagerListAdapter(HealthSimpleClueMnagerActivity.this, list);
            lstv.setAdapter(ladapter);
            ladapter.notifyDataSetChanged();
            GetClueData.clue= null;
        }else{
            onRefresh();
        }

    }
}
